# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = 2^(nwave-1:1)
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
wave
tperiod
2:1
nwave-1
tperiod = 2^c(nwave-1:1)
tperiod
tperiod = nwave-1:1
tperiod
nwave-1:1
2:1
nwave:1
(nwave-1):1
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = (nwave-1):1; tperiod = 2^tperiod
tperiod
# Shift time (min period of square wave)
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = (nwave-1):1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
nPeriods
nshifts/tperiod
seq(1,2,2)
seq(1,2)
seq(1,4,2)
?seq
seq(1,4,by=2)
seq(1,3,by=2)
seq(1,2,by=2)
# Shift time (min period of square wave)
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = (nwave-1):1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
nPeriods[i] = nshifts/tperiod[i]
for(j in seq(1, nPeriods[i], by = 2)){
wave[i, (j-1)*tperiod[i]+1:j*tperiod[i]] = frac
}
}
wave[i, (j-1)*tperiod[i]+1:j*tperiod[i]]
i
(j-1)*tperiod[i]+1:j*tperiod[i]
j
nPeriods
tperiod
# Shift time (min period of square wave)
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = nwave:1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
nPeriods[i] = nshifts/tperiod[i]
for(j in seq(1, nPeriods[i], by = 2)){
wave[i, (j-1)*tperiod[i]+1:j*tperiod[i]] = frac
}
}
j
wave
nPeriods
i = 2
nPeriods[i]
nwave
seq(1, nPeriods[i], by = 2)
seq(2, nPeriods[i], by = 2)
seq(2, nPeriods[i+1], by = 2)
# Shift time (min period of square wave)
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = nwave:1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, (j-1)*tperiod[i]+1:j*tperiod[i]] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
wave
i
(j-1)*tperiod[i]+1:j*tperiod[i]
j
(j-1)*tperiod[i]
j*tperiod[i]
seq((j-1)*tperiod[i]+1, j*tperiod[i])
# Shift time (min period of square wave)
tshift = 50; nshifts = 8;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = nwave:1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
wave
# Shift time (min period of square wave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)
# Period sizes (decreasing)
tperiod = nwave:1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
wave
# Shift time (min period of square wave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = nwave:1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(NULL, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
nwave
tperiod
# Shift time (min period of square wave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):1; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(NULL, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
tperiod
# Shift time (min period of square wave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):0; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(NULL, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, 3)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
tperiod
# Shift time (min period of square wave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):0; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(NULL, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, nwave)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
nPeriods
i=2
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
nPeriods
nPeriods[i]
j=1
seq((j-1)*tperiod[i]+1, j*tperiod[i])
wave
nwave
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):0; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(NULL, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, nwave)
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):0; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(-1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, nwave)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
wave
tshift = 50; nshifts = 16;
# Square wave range and numbers
frac = 0.5; nwave = log2(nshifts)+1
# Period sizes (decreasing)
tperiod = (nwave-1):0; tperiod = 2^tperiod
# Waves (define sequence of fr)
wave = matrix(1, nrow = nwave, ncol = nshifts)
nPeriods = matrix(0, 1, nwave)
for(i in 2:nwave){
# No. periods and starting amplitude
nPeriods[i] = nshifts/tperiod[i]
ampLast = 1
for(j in 1:nPeriods[i]){
wave[i, seq((j-1)*tperiod[i]+1, j*tperiod[i])] = ampLast
if(ampLast == 1){
ampLast = frac
} else {
ampLast = 1
}
}
}
wave
4.22/4
1.96*2
4.72/3.92
samps
tperiod
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
j
for (j in 1:numRuns) {
# Number of samples introduced at each time
nsamps = nSamps[j]
samps = nsamps*rep(1, nsegs)
# Coalescent events in each trajectory
nc = rep(0, numTraj)
# Create folder for traj specific results
trajName = paste(c('sqwave_', nsamps-1), collapse = '')
dir.create(file.path(this.dir, trajName))
pathf = paste(c(this.dir, '/', trajName, '/'), collapse = "")
# Define square wave population size
sqwave_traj <- function (t, wav = wave[idTraj[j],], N = 1000, tseg = samptimes)
{
popsize = rep(N, length(t))
for(i in 1:nsegs){
popsize[t <= tseg[i] & t > tseg[i-1]] = wav[i]*N
}
return(popsize)
}
traj = sqwave_traj
for(i in 1:numTraj){
# Simulate genealogy and get all times
gene = coalsim(samp_times = samptimes, n_sampled = samps, traj = traj, lower_bound = 10, method = "thin")
coal_times = gene$coal_times
coalLin = gene$lineages
# TMRCA and no. coalescent events
tmax = max(coal_times)
nc[i] = length(coal_times)
# Obtain true trajectory across time
t = seq(0, tmax, length=10^4)
y = traj(t)
# Export trajectory specific data for Matlab
tableWrite(coal_times, paste(c('coaltimes', i, '.csv'), collapse = ''), pathf)
tableWrite(coalLin, paste(c('coalLin', i, '.csv'), collapse = ''), pathf)
}
# No. samples, coalescences and times
tableWrite(nc, 'nc.csv', pathf)
tableWrite(samptimes, 'samptimes.csv', pathf)
tableWrite(samps, 'sampIntro.csv', pathf)
# Data on square wave models
tableWrite(numRuns, 'numRuns.csv', pathf)
tableWrite(numTraj, 'numTraj.csv', pathf)
tableWrite(wave, 'wave.csv', pathf)
tableWrite(idTraj, 'idTraj.csv', pathf)
}
j
j = 10
# Number of samples introduced at each time
nsamps = nSamps[j]
samps = nsamps*rep(1, nsegs)
# Coalescent events in each trajectory
nc = rep(0, numTraj)
# Create folder for traj specific results
trajName = paste(c('sqwave_', nsamps-1), collapse = '')
dir.create(file.path(this.dir, trajName))
pathf = paste(c(this.dir, '/', trajName, '/'), collapse = "")
# Define square wave population size
sqwave_traj <- function (t, wav = wave[idTraj[j],], N = 1000, tseg = samptimes)
{
popsize = rep(N, length(t))
for(i in 1:nsegs){
popsize[t <= tseg[i] & t > tseg[i-1]] = wav[i]*N
}
return(popsize)
}
traj = sqwave_traj
for(i in 1:numTraj){
# Simulate genealogy and get all times
gene = coalsim(samp_times = samptimes, n_sampled = samps, traj = traj, lower_bound = 10, method = "thin")
coal_times = gene$coal_times
coalLin = gene$lineages
# TMRCA and no. coalescent events
tmax = max(coal_times)
nc[i] = length(coal_times)
# Obtain true trajectory across time
t = seq(0, tmax, length=10^4)
y = traj(t)
# Export trajectory specific data for Matlab
tableWrite(coal_times, paste(c('coaltimes', i, '.csv'), collapse = ''), pathf)
tableWrite(coalLin, paste(c('coalLin', i, '.csv'), collapse = ''), pathf)
}
# No. samples, coalescences and times
tableWrite(nc, 'nc.csv', pathf)
tableWrite(samptimes, 'samptimes.csv', pathf)
tableWrite(samps, 'sampIntro.csv', pathf)
# Data on square wave models
tableWrite(numRuns, 'numRuns.csv', pathf)
tableWrite(numTraj, 'numTraj.csv', pathf)
tableWrite(wave, 'wave.csv', pathf)
tableWrite(idTraj, 'idTraj.csv', pathf)
# Simulate coalescent process with heterochronous sampling times
# Assumptions and modifications
# - does runs across various sample numbers (data)
# - deposits batch runs in a single folder
# - simulate a single rate-shift
# - uses phylodyn package of Karcher 2016 et al
# - samples placed isochronously at 0 and tshift
# - altChange gives fraction of fall after shift
# - removed several outputs e.g. tree
# Clean the workspace and console
closeAllConnections()
rm(list=ls())
cat("\014")
graphics.off()
# Packages for phylodyn
library("sp")
library("devtools")
library("INLA")
library("spam")
library("ape")
library("phylodyn")
# Set working directory to source
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)
# Function to write simple csv files to correct path
tableWrite <- function(val, name, pathname) {
# Add path to name
str0 <- paste(c(pathname, name), collapse = "")
# Write table
write.table(val, str0, row.names=FALSE, col.names=FALSE, sep=",")
}
# Main code for heterochronous simulations ----------------------------------------------------------
# Shift time and fraction of change
tshift = 500; fr = 0.5
# Null model probability and weights
P0 = 0.5; wt = c(P0, 1-P0)
# No. trajectories with or without shift
numTraj = 500
# Randomly generate these trajectories
fracs = sample(c(1, fr), numTraj, replace=TRUE, prob = wt)
# Range of sample numbers to loop across
nSamps = seq(51, 501, 50)
numRuns = length(nSamps)
j = 2
samp_times = seq(0, tshift, length.out = 2)
# Number of samples introduced at each time
nsamps = nSamps[j]
samps = c(ceiling(nsamps/2), floor(nsamps/2))
# Coalescent events in each trajectory
nc = rep(0, numTraj)
nc
i = 10
# Define rate-shift with population fall
shift_traj <- function (t, frac = fracs[i], ts = tshift)
{
result = rep(0, length(t))
result[t <= tshift] <- 1000
result[t >= tshift] <- frac*1000
return(result)
}
traj = shift_traj
fracs[i]
shift_traj(1000)
shift_traj(1)
i = 20
fracs[i]
shift_traj(1)
shift_traj(1000)
tshift
shift_traj(500)
shift_traj(499)
nc
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
nSamps = seq(21, 101, 20)
nSamps
nSamps = seq(21, 101, 10)
nSamps
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
prob
rep(1, nwave)/nwave
seq(51, 201, 10)
traj
traj(100)
traj(1000)
traj(10000)
sampIntro
samp_tomes
samp_times
samptimes
traj(samptimes)
traj(1600)
wave
source('~/Desktop/Imperial/2018/Term 2/Code/skymodsel/using phylodyn/batchSquareWave.R')
